<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>
        配置用户的角色列表
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/assets/admin/lib/xm-select.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<style  type="text/css">
    <!-- 控制xm-select不换行   -->
    xm-select > .xm-label .xm-label-block{
        display: inline-flex;
    }
    #transitCode .label-content{
        max-height: 38px;
        overflow: hidden;
    }
</style>

<body>
<input type="hidden" th:value="${userId == null ?'':userId}" id="userId">
<div class="x-body" >
    <div class="layui-col-md3">
        <div class="layui-card">
            <form class="layui-form" action="">
                <div id="roleSelect" class="xm-select-demo" sub-name="roleIds"></div>
                <div class="layui-card-body" style="text-align: center;margin-top:150px;">
                    <button class="layui-btn" lay-submit lay-filter="update">
                        <i class="layui-icon">&#xe642;</i>修改
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script th:src="@{/assets/admin/js/jquery.min.js}"></script>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script>
    let demo1;
    $(function () {
        //渲染多选
        demo1 = xmSelect.render({
            el: '#roleSelect',
            filterable: true,
            searchTips: '你想搜索什么角色？',//搜索标题
            paging: true, //开启分页
            pageSize: 5,  //每页展示5条数据
            autoRow: true, //自动换行
            toolbar: {  //展示工具
                    show: true,
                    list: [ 'ALL', 'CLEAR', 'REVERSE' ]
                },
            theme: { //选中的主题颜色
                color: '#1cbbb4',
            },
            size: 'medium', //
            data: []
        });
        getData();
        initxmSelect();
        layui.use(['jquery', 'form'], function () {
            $ = layui.jquery;//jquery
            form = layui.form;

            form.on('submit(update)', function (obj) {

                console.log(obj);
                let roles = demo1.getValue();
                let roleIds = new Array();
                if(roles.length > 0){
                    roles.forEach((item,index)=>{
                        roleIds.push(item.value);
                    });
                }
                $.ajax({
                    url: "/user/updateUserRoles",
                    type: "post",
                    data:{
                        "userId":$("#userId").val(),
                        "roleIds": roleIds
                    },
                    success:function(data){
                        if (data.code == 200) {
                            layer.alert(data.message, {icon: 6}, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        } else {
                            layer.alert(data.message, {icon: 5}, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            });
                        }
                    }
                })
                //阻止默认事件
                return false;
            });
        });
    });

    function getData() {
        $.ajax({
            url: "/role/getRolesByUserId",
            type: "get",
            data: {"userId": $("#userId").val()},
            success: function (data) {
                console.log(data);
                demo1.update({
                    data: data.result
                })
            }
        });
    }
    //初始化xm-select
    function initxmSelect(){
        let xmSelects = $(".xm-select-demo");
        $.each(xmSelects,function(index,item){
            $(item).find("input[name='select']").attr('name',$(item).attr('sub-name'));
        });
    }

</script>
</html>